package io.gitee.zicai.bus.sample.cmd;

import java.io.Serializable;
import java.util.Arrays;
import java.util.function.BiConsumer;

/**
 * CmdEnum
 *
 * @author zicai
 * @since 2021-04-02
 */
public enum CmdEnum implements Serializable {

    hello("hello", ICmdSender::sendHello, ICmdReceiver::receiveHello),
    other("other", ICmdSender::other, ICmdReceiver::other);

    public String cmd;
    public BiConsumer<ICmdSender, CmdSendParam> cmdSender;
    public BiConsumer<ICmdReceiver, CmdReceiveData> cmdReceiver;

    CmdEnum(String cmd, BiConsumer<ICmdSender, CmdSendParam> cmdSender, BiConsumer<ICmdReceiver, CmdReceiveData> cmdReceiver) {
        this.cmd = cmd;
        this.cmdSender = cmdSender;
        this.cmdReceiver = cmdReceiver;
    }

    public static CmdEnum parse(String cmd) {
        return Arrays.stream(CmdEnum.values())
                .filter(v -> v.cmd.equals(cmd))
                .findFirst()
                .orElse(other);
    }
}
